<template>
  <el-dialog
    title="修改"
    :visible.sync="data.dialog_info_flag"
    @close="close"
    width="580px"
    @open="openDialog"
  >
    <el-form :model="data.form" label-position="left" ref="addArticleForm">
      <el-form-item label="类别: " :label-width="data.formLabelWidth" prop="category">
        <el-select v-model="data.form.category" placeholder="请选择">
          <el-option
            v-for="item in data.categoryOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题: " :label-width="data.formLabelWidth" prop="title">
        <el-input v-model="data.form.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="内容: " :label-width="data.formLabelWidth" prop="content">
        <el-input type="textarea" v-model="data.form.content" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="danger" :loading="data.submitLoading" @click="submit">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reactive, ref, watchEffect } from "@vue/composition-api";
import {addArticle, editArticle, getArticleList} from "../../../api/article";

export default {
  name: "dialogInfo",
  props: {
    flag: {
      type: Boolean,
      default: false
    },
    category: {
      type: Array,
      default: () => []
    },
    id: {
      type: String,
      default: ""
    }
  },
  beforeUpdate() {
    console.log("dialog/index.vue beforeUpdate"); // prop变化会触发这个生命周期,但并不会像react一样,再次执行一遍setup函数
  },
  setup(props, { emit, root, refs }) {
    /** === === === === === === data === === === === === ===*/
    //#region
    const data = reactive({
      dialog_info_flag: false,
      formLabelWidth: "70px",
      form: {
        category: "",
        title: "",
        content: ""
      },
      categoryOption: [],
      submitLoading: false
    });
    const submitLoading = ref(false);
    const addArticleForm = ref(null);
    //#endregion
    /** === === === === === === watch === === === === === ===*/
    //#region
    watchEffect(() => (data.dialog_info_flag = props.flag));
    //#endregion
    /** === === === === === === method === === === === === ===*/
    //#region
    const close = () => {
      data.dialog_info_flag = false;
      emit("update:flag", false);
      resetForm();
    };
    const openDialog = () => {
      data.categoryOption = props.category;
      getInfo();
    };
    const getInfo = () => {
      const requestData = {
        pageNumber: 1,
        pageSize: 1,
        id: props.id
      };
      getArticleList(requestData).then(res => {
        data.form = {
          category: res.categoryId,
          title: res.title,
          content: res.content
        };
      });
    };
    const resetForm = () => {
      // Object.keys(data.form).forEach(key => {
      //   data.form[key] = "";
      // });
      addArticleForm.value.resetFields();
    };
    const submit = () => {
      data.submitLoading = true;
      const requestData = {
        id: props.id,
        category: data.form.category,
        title: data.form.title,
        content: data.form.content
      };

      //todo 检验各字段的值是否合法

      editArticle(requestData)
        .then(data => {
          root.$message({
            message: "添加成功"
          });
          data.submitLoading = false;
          close();
          // addArticleForm.value.resetFields(); // ← emmm 莫名无效
          // resetForm();
          emit("getList")
        })
        .catch(err => {
          data.submitLoading = false;
          close();
          console.log("addArticleForm.value:", addArticleForm.value);
          // addArticleForm.value.resetFields();
          // resetForm();
        });
    };
    //#endregion

    return {
      data,
      addArticleForm,
      close,
      openDialog,
      submit
    };
  }
};
</script>

<style lang="scss" scoped></style>
